const nodemailer = require('nodemailer');

const source = "onecubetech@163.com";
const token = 'TGFGDPBVFDPBVXXV';
const server = 'smtp.163.com';

function send(target, title, content) {
  return new Promise((resolve, reject) => {
    let transporter = nodemailer.createTransport({
      host: server,
      port: 465,
      secure: true,
      auth: {
        user: source,
        pass: token
      }
    });
    
    let mailOptions = {
      from: source,
      to: target,
      subject: title,
      text: content
    };
    
    transporter.sendMail(mailOptions, (err, res) => {
      if (err) {
        console.log(err);
        reject(err);
      } else {
        console.log('Email sent: ' + res.response);
        resolve(res.response);
      }
    });
  })
}

module.exports = send;